package cn.edu.swu;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;

public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String user = request.getParameter("user");
        String pass = request.getParameter("pass");
        String code = request.getParameter("code");
        response.setContentType("text/html");
        HttpSession session=request.getSession(true);

        if(code==null||!code.equalsIgnoreCase((String) session.getAttribute(AuthCodeServlet.AUTH_CODE))){
            response.sendRedirect("./index.html");
            return;
        }

        if(user!=null&&user.equals("Tom")){
            if(pass!=null&&pass.equals("123456")){
                session.setAttribute(AuthFilter.LOGIN_STATUS,AuthStatus.LOGIN_SUCCESS);
                //返回一个重定向
                response.sendRedirect("./main");
                return;
            }
        }
        //返回HTML错误
        //response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "用户名或密码错误");
        try (Writer writer=response.getWriter()){
            response.setHeader("Refresh","3;url=./index.html");
            writer.write("<center><h1>用户名或者密码不正确，3秒后自动跳转<h1></center>");
        }
    }
    private String printParameters(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        sb.append("<table style='width:70%' cellpadding='5px' cellspacing='20px'>");
        sb.append("<tr style='background-color:#336699;color=#FFF'><th>Header名称</th><th>Header值</th></tr>");
        Enumeration<String> names = request.getHeaderNames();
        while(names.hasMoreElements()){
            String name=names.nextElement();
            sb.append(String.format("<tr style='background-color:#eee'><td>%s</td><td>%s</td></tr>",
                    name,request.getParameter(name)));
            //sb.append(name+":"+request.getHeader(name)+"<br>");
        }
        sb.append("</table>");
        return sb.toString();
    }
    private String printHeaders(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        sb.append("<table style='width:70%' cell-padding='5px' cells pacing='1em'>");
        sb.append("<tr style='background-color:#336699;color=#fff'><th>Header名称</th><th>Header值</th></tr>");
        Enumeration<String> names = request.getParameterNames();
        while(names.hasMoreElements()){
            String name=names.nextElement();
            sb.append(String.format("<tr style='background-color:#eee'><td>%s</td><td>%s</td></tr>",
                    name,request.getHeader(name)));
            //sb.append(name+":"+request.getHeader(name)+"<br>");
        }
        sb.append("</table>");
        return sb.toString();
    }

}
